#include "MainMenuState.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "HighScoresState.h"
#include "GameOverState.h"
#include "GameplayState.h"
#include "OptionsState.h"
#include "TutorialState.h"
#include "SaveState.h"
#include "PauseState.h"
#include "IntroState.h"
#include "../Game.h"
#include "CreditState.h"
#include "LoadState.h"

CMainMenuState* CMainMenuState::GetInstance(void)
{
	static CMainMenuState s_Instance;
	return &s_Instance;
}

CMainMenuState::CMainMenuState(void)
{
	m_nBackGroundImgID		= -1;
	m_nMenuCursor			= -1;
	m_nBackGroundSfxID		= -1;
	m_nBackGroundMusicID	= -1;
	m_nCursorMove			= 1;
	m_nOptionsLogoOff		= -1;
	m_nOptionsLogoOn		= -1;
	m_nHighScoreLogoOff		= -1;
	m_nHighScoreLogoOn		= -1;
	m_nLoadLogoOff			= -1;
	m_nLoadLogoOn			= -1;
}

CMainMenuState::~CMainMenuState(void) { }

void CMainMenuState::Enter(void)
{
//setup the state
	m_pD3D	= CSGD_Direct3D::GetInstance();
	m_pTM	= CSGD_TextureManager::GetInstance();
	m_pDI	= CSGD_DirectInput::GetInstance();
	m_pXA	= CSGD_XAudio2::GetInstance();
	m_pTM	= CSGD_TextureManager::GetInstance();

	m_pFont = new CBitmapFont();
	m_pFont->Load("resources/XML/DeadlyShadowsFont.xml");

//IMAGES
	this->m_nBackGroundImgID = m_pTM->LoadTexture(L"resources/graphics/mainMenu.jpg", D3DCOLOR_XRGB(0, 0, 0));
	this->m_nOptionsLogoOff = m_pTM->LoadTexture(L"resources/graphics/options_menu_iconOFF.png", D3DCOLOR_XRGB(0, 0, 0));
	this->m_nOptionsLogoOn = m_pTM->LoadTexture(L"resources/graphics/options_menu_iconON.png", D3DCOLOR_XRGB(0, 0, 0));
	this->m_nLoadLogoOn = m_pTM->LoadTexture(L"resources/graphics/load_menu_icon_ON.png", D3DCOLOR_XRGB(0, 0, 0));
	this->m_nLoadLogoOff = m_pTM->LoadTexture(L"resources/graphics/load_menu_icon_OFF.png", D3DCOLOR_XRGB(0, 0, 0));
	//this->m_nMenuCursor = m_pTM->LoadTexture(_T("resource/graphics/CJM_SGD_MenuCursor.png"), D3DCOLOR_XRGB(0,0,0));
//SOUND
	//this->m_nBackGroundSfxID = m_pXA->SFXLoadSound(_T("resource/sounds/CJM_SFXBackGround.xwm"));
	//this->m_nBackGroundMusicID = m_pXA->MusicLoadSong(_T("resource/sounds/CJM_Music.xwm"));
	
	//if (!m_pXA->MusicIsSongPlaying(m_nBackGroundMusicID))
	//{
	//	m_pXA->MusicStopSong( m_nBackGroundMusicID );
	//	m_pXA->MusicPlaySong(m_nBackGroundMusicID, true);
	//}
}

void CMainMenuState::Exit(void)
{
//IMAGES
	if (this->m_nMenuCursor != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nMenuCursor);
		this->m_nMenuCursor = -1;
	}
	if (this->m_nBackGroundImgID != -1)
	{
		this->m_pTM->UnloadTexture( this->m_nBackGroundImgID );
		this->m_nBackGroundImgID = -1;
	}
	if (this->m_nOptionsLogoOff != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nOptionsLogoOff);
		this->m_nOptionsLogoOff = -1;
	}
	if (this->m_nOptionsLogoOn != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nOptionsLogoOn);
		this->m_nOptionsLogoOn = -1;
	}
	if (this->m_nLoadLogoOn != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nLoadLogoOn);
		this->m_nLoadLogoOn = -1;
	}
	if (this->m_nLoadLogoOff != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nLoadLogoOff);
		this->m_nLoadLogoOff = -1;
	}

//SOUND
	if( this->m_nBackGroundMusicID != -1 )
	{
		this->m_pXA->MusicStopSong( this->m_nBackGroundMusicID );
		this->m_pXA->MusicUnloadSong( this->m_nBackGroundMusicID );
		this->m_nBackGroundMusicID = -1;
	}
	if (this->m_nBackGroundSfxID != -1)
	{
		this->m_pXA->SFXStopSound(this->m_nBackGroundSfxID);
		this->m_pXA->SFXUnloadSound(this->m_nBackGroundSfxID);
		this->m_nBackGroundSfxID = -1;
	}
//FONTS
	if (m_pFont != nullptr)
	{
		delete m_pFont;
		m_pFont = nullptr;
	}
}

bool CMainMenuState::Input(void)
{

	if (m_pDI->KeyPressed(DIK_LEFT))
	{
		if (m_nCursorMove == 1)
		{
			m_nCursorMove = 3;
		}
		m_nCursorMove--;
	}
	if (m_pDI->KeyPressed(DIK_RIGHT))
	{
		if (m_nCursorMove == 2)
		{
			m_nCursorMove = 0;
		}
		m_nCursorMove++;
	}
	if (m_nCursorMove== 1)
	{
		if (m_pDI->KeyReleased(DIK_RETURN))
		{
			CGame::GetInstance()->AddState(COptionsState::GetInstance());
			CGame::GetInstance()->RemoveState(this);
			return true;
		}
	}
	if (m_nCursorMove == 2)
	{
		if (m_pDI->KeyReleased(DIK_RETURN))
		{
			CGame::GetInstance()->AddState(CLoadState::GetInstance());
			CGame::GetInstance()->RemoveState(this);
			return true;
		}
		return true;
	}


//TESTING THE STATES----------------------------------------------------------
	//if (this->m_pDI->KeyPressed(DIK_Z))
	//{
	//	CGame::GetInstance()->ChangeState(CGameplayState::GetInstance());
	//	return true;
	//}
	if (this->m_pDI->KeyPressed(DIK_X))
	{
		CGame::GetInstance()->AddState(CCreditState::GetInstance());
		CGame::GetInstance()->RemoveState(this);
		return true;
	}
	//if (this->m_pDI->KeyPressed(DIK_C))
	//{
	//	CGame::GetInstance()->AddState(COptionsState::GetInstance());
	//	CGame::GetInstance()->RemoveState(this);
	//	return true;
	//}
	if (this->m_pDI->KeyPressed(DIK_V))
	{
		CGame::GetInstance()->AddState(CGameOverState::GetInstance());
		CGame::GetInstance()->RemoveState(this);
		return true;
	}
	if (this->m_pDI->KeyPressed(DIK_B))
	{
		CGame::GetInstance()->AddState(CHighScoresState::GetInstance());
		CGame::GetInstance()->RemoveState(this);
		return true;
	}
	if (this->m_pDI->KeyPressed(DIK_N))
	{
		CGame::GetInstance()->AddState(CGameplayState::GetInstance());
		CGame::GetInstance()->RemoveState(this);
		return true;
	}
	//if (this->m_pDI->KeyPressed(DIK_M))
	//{
	//	CGame::GetInstance()->ChangeState(CSaveState::GetInstance());
	//	return true;
	//}
	//if (this->m_pDI->KeyPressed(DIK_A))
	//{
	//	CGame::GetInstance()->ChangeState(CPauseState::GetInstance());
	//	return true;
	//}
	if (this->m_pDI->KeyPressed(DIK_S))
	{
		return true;
	}
//-----------------------------------------------------------------------
	return true;
}

void CMainMenuState::Update(float fElapsedTime)
{
	this->m_pXA->Update();
}

void CMainMenuState::Render(void)
{
	this->m_pTM->Draw(m_nBackGroundImgID, 0, 0, 0.6f, 0.6f, nullptr, 0, 0, 0);
	this->m_pD3D->GetSprite()->Flush();

	if (m_nCursorMove == 1)
	this->m_pTM->Draw(m_nOptionsLogoOn, 0, 0, 0.75f, 1.0f,nullptr, 0,0,0);
	else 
	this->m_pTM->Draw(m_nOptionsLogoOff, 0, 0, 0.75f, 1.0f,nullptr, 0,0,0);
	if (m_nCursorMove == 2)
	{
		this->m_pTM->Draw(m_nLoadLogoOn, 300, 0, 0.75f, 1.0f,nullptr, 0,0,0);
	}
	else
	{
	this->m_pTM->Draw(m_nLoadLogoOff, 300, 0, 0.75f, 1.0f,nullptr, 0,0,0);		
	}


//FONT=============================================================
	this->m_pFont->Print("MAIN MENU", 100, 100, 1.0f, D3DXCOLOR(0,255,0,255));
	this->m_pFont->Print("Credits TEST .. press X", 125, 125, 1.0f, D3DXCOLOR(0,255,0,255));
	this->m_pFont->Print("Options TEST .. press C", 125, 150, 1.0f, D3DXCOLOR(0,255,0,255));
	this->m_pFont->Print("GameOver TEST .. press V", 125, 175, 1.0f, D3DXCOLOR(0,255,0,255));
	this->m_pFont->Print("HighScore TEST .. press B", 125, 200, 1.0f, D3DXCOLOR(0,255,0,255));
	this->m_pFont->Print("GameplayState TEST .. press N", 125, 225, 1.0f, D3DXCOLOR(0,255,0,255));

//TESTING THE STATES------------------------------------------------------------------------
}